home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / XPK / Source / shell / xDir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  3.9 KB  |  182 lines

  1. #define NAME     "xDir"
  2. #define REVISION "3"
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        xDir
  7.     Author:        SDI (before 1.2 Urban Dominik Müller)
  8.     Distribution:    PD
  9.     Description:    xpk file lister
  10.     Compileropts:    -
  11.     Linkeropts:    -l xpkmaster
  12.  
  13.  1.0    : first public release
  14.  1.1    : Enforcer hit fixed, version string added, wrote documentation
  15.  1.2   29.11.96 : reworked for new includes
  16.  1.3   24.04.97 : fixed lots of errors, made a lot shorter, easier
  17. */
  18.  
  19. #include "SDI_defines.h"
  20. #define SDI_TO_ANSI
  21. #include "SDI_ASM_STD_protos.h"
  22. #include <pragma/exec_lib.h>
  23. #include <pragma/dos_lib.h>
  24. #include <pragma/xpkmaster_lib.h>
  25. #include <exec/memory.h>
  26.  
  27. #ifdef __MAXON__
  28.   #define __asm
  29. #endif
  30.  
  31. #define MAXLINES 2000
  32.  
  33. struct Library *    XpkBase    = 0;
  34. struct FileInfoBlock *    fib    = 0;
  35. STRPTR            Line[MAXLINES];
  36. ULONG            utot    = 0,
  37.             ctot    = 0,
  38.             Lines    = 0;
  39.  
  40. void exitem(STRPTR name);
  41. void exfile(void);
  42. void FreeLines(void);
  43.  
  44. void end(STRPTR text)
  45. {
  46.   if(text)    printf(text);
  47.   if(XpkBase)    CloseLibrary(XpkBase);
  48.   if(fib)    FreeMem(fib, sizeof(struct FileInfoBlock));
  49.   FreeLines();
  50.   exit(text ? 10 : 0);
  51. }
  52.  
  53. void FreeLines(void)
  54. {
  55.   while(Lines)
  56.     FreeMem(Line[--Lines], 200);
  57. }
  58.  
  59. void main(int argc, char *argv[])
  60. {
  61.   LONG i, ratio;
  62.  
  63.   if(argc == 2 && argv[1][0] == '?')
  64.     end("Usage: xDir files/dirs\n");
  65.  
  66.   if(!(XpkBase = OpenLibrary(XPKNAME, 3)))
  67.     end("Cannot open " XPKNAME "\n");
  68.  
  69.   if(argc < 2)
  70.     argv[argc++] = "";
  71.  
  72.   if(!(fib = (struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock), MEMF_ANY)))
  73.     end("Not enough memory\n");
  74.  
  75.   printf("Original  Packed  Ratio Type Protection Name\n"
  76.          "-------- -------- ----- ---- ---------- ----\n");
  77.  
  78.   for(i = 1; i < argc;)
  79.   {
  80.     exitem(argv[i]);
  81.     if(++i < argc)
  82.       printf("\n");
  83.   }
  84.  
  85.   ratio = (utot && utot < ctot) ? 100 - (100 * ctot) / utot : 0;
  86.   printf("-------- -------- -----\n%8ld %8ld  %2ld%%\n", utot, ctot, ratio);
  87.  
  88.   end(0);
  89. }
  90.  
  91. void exitem(STRPTR name)
  92. {
  93.   BPTR lock, prev;
  94.   ULONG i;
  95.  
  96.   if(!(lock = Lock(name, ACCESS_READ)))
  97.   {
  98.     printf("Error %ld locking %s\n", IoErr(), name);
  99.     return;
  100.   }
  101.  
  102.   if(!Examine(lock, fib))
  103.   {
  104.     UnLock(lock);
  105.     printf("Error %ld locking %s\n", IoErr(), name);
  106.     return;
  107.   }
  108.  
  109.   if(fib->fib_DirEntryType < 0)
  110.   {
  111.     UnLock(lock);
  112.     name[strlen(name)-strlen(fib->fib_FileName)] = 0; /* cut the filename */
  113.     if(!(lock = Lock(name, ACCESS_READ)))
  114.     {
  115.       printf("Error locking %s\n", name);
  116.       return;
  117.     }
  118.     prev = CurrentDir(lock);
  119.     exfile();
  120.     UnLock(CurrentDir(prev));
  121.   }
  122.   else
  123.   {
  124.     prev = CurrentDir(lock);
  125.  
  126.     while(ExNext(lock, fib))
  127.       exfile();
  128.     UnLock(CurrentDir(prev));
  129.   }
  130.  
  131.   for(i = 0; i < Lines; ++i)
  132.   {
  133.     printf(Line[i]);
  134.     if(SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  135.       end(" *** Break\n");
  136.   }
  137.   FreeLines();
  138. }
  139.  
  140. void exfile(void)
  141. {
  142.   struct XpkFib xfib;
  143.   UBYTE prot[9];
  144.   ULONG clen = 0, ulen = 0;
  145.   LONG i;
  146.   STRPTR buf;
  147.  
  148.   if(SetSignal (0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  149.     end(" *** Break\n");
  150.   if(Lines >= MAXLINES-1 || !(buf = (STRPTR) AllocMem(200, MEMF_ANY)))
  151.     end("Buffer overflow or no memory\n");
  152.   else
  153.   {
  154.     prot[0] = fib->fib_DirEntryType < 0 ? '-' : 'D';
  155.     for(i = 0; i < 8; i++)
  156.       prot[8 - i] = (fib->fib_Protection ^ 0x0f) & (1 << i) ? "dewrapsh"[i] : '-';
  157.  
  158.     if(fib->fib_EntryType >= 0)
  159.       sprintf(buf,  "%8s                     %.9s  %-20s\n",
  160.     "<Dir>", prot, fib->fib_FileName);
  161.     else if(!XpkExamineTags(&xfib, XPK_InName, fib->fib_FileName, TAG_DONE) &&
  162.     xfib.xf_Type == XPKTYPE_PACKED)
  163.       sprintf(buf,   "%8ld %8ld  %2ld%%  %4s  %.9s  %-20s\n",
  164.     ulen = xfib.xf_ULen, clen = xfib.xf_CLen, xfib.xf_Ratio,
  165.     xfib.xf_Packer, prot, fib->fib_FileName);
  166.     else
  167.       sprintf(buf, "%8ld                     %.9s  %-20s\n",
  168.     clen = ulen = fib->fib_Size, prot, fib->fib_FileName);
  169.  
  170.     utot += ulen;
  171.     ctot += clen;
  172.  
  173.     /* sort and insert the new line */
  174.     for(i = Lines - 1; i >= 0 && stricmp(Line[i] + 40, buf + 40) > 0; i--)
  175.       Line[i + 1] = Line[i];
  176.     Line[i + 1] = buf;
  177.  
  178.     ++Lines;
  179.   }
  180. }
  181.  
  182.